package com.sanjiahantang.ythshopping.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.sanjiahantang.ythshopping.R;

import com.sanjiahantang.ythshopping.model.NearbyShop;
import com.sanjiahantang.ythshopping.model.NearbyShopModel;
import com.sanjiahantang.ythshopping.presenter.NearbyShopPresenter;
import com.sanjiahantang.ythshopping.utils.Utils;
import com.sanjiahantang.ythshopping.views.NearbyShopView;

import java.util.List;

public class NearbyShopActivity extends BaseActivity implements AMapLocationListener, NearbyShopView {

    public AMapLocationClient client = null;
    private AMapLocationClientOption locationOption;

    private SwipeRefreshLayout refreshLayout;
    private RecyclerView nearbyList;

    private double longitude;
    private double latitude;

    private NearbyShopPresenter presenter;


    private LinearLayoutManager linearManager;
    private List<NearbyShop.ShopList> datas;

    private TextView title;
    private FrameLayout back;

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_nearby);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.nearby_refresh);
        nearbyList = (RecyclerView) findViewById(R.id.nearby_shop_list);
        linearManager = new LinearLayoutManager(this);
        client = new AMapLocationClient(getApplicationContext());
        client.setLocationListener(this);
        nearbyList.setLayoutManager(linearManager);
        locationOption = new AMapLocationClientOption();
        locationOption.setOnceLocation(true);
        locationOption.setOnceLocationLatest(true);
        client.setLocationOption(locationOption);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
        }else {
            refreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    refreshLayout.setRefreshing(true);
                }
            });
            client.startLocation();


        }
        presenter = new NearbyShopPresenter(this);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.getNearbyShopList(NearbyShopActivity.this, longitude, latitude);
            }
        });

    }

    @Override
    public void initTitle() {
        super.initTitle();
        title = (TextView) findViewById(R.id.title);
        back = (FrameLayout) findViewById(R.id.fl_Left);
        title.setText("附近商家");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            if (requestCode == 1){
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

                    refreshLayout.post(new Runnable() {
                        @Override
                        public void run() {
                            refreshLayout.setRefreshing(true);
                        }
                    });
                    client.startLocation();
                }else {

                    showToast( "未授予权限，无法进行定位服务！");
                }
            }
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null){
            if (aMapLocation.getErrorCode() == 0){
                longitude = aMapLocation.getLongitude();
                latitude = aMapLocation.getLatitude();
                presenter.getNearbyShopList(this, longitude, latitude);
            }

        }

    }

    @Override
    public void getNearbyShop(NearbyShopModel model) {
        refreshLayout.setRefreshing(false);
        if (model.getCode().equals("1")){
            datas = model.getData().getShop_list();

        }else {
            Utils.showToast(this, model.getMsg());
        }
    }

    @Override
    public void getNearbyShopList(NearbyShopModel model) {

    }
}
